<?php


namespace app\admin\controller\Admin;


use app\admin\controller\BaseController;
use app\admin\model\Admin\BannerModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\facade\Db;
use think\Request;

class Banner extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getBannerList(Request $request)
    {
        $where = [];
        $param = $request->param();
        $page = $param['page'];
        $limit = $param['limit'];
        $banner_type_id = !empty($param['banner_type_id']) ? $param['banner_type_id'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($page)){
            $page = 1;
        }
        if(empty($limit)){
            $limit = 10;
        }

        if(!empty($banner_type_id)){
            array_push($where,['banner_type_id','=',$banner_type_id]);
        }
        if(!empty($is_show)){
            if($is_show == 1){
                array_push($where,['is_show','=',1]);
            }
            if($is_show == 2){
                array_push($where,['is_show','=',0]);
            }
        }

        $list = BannerModel::getBannerListWhere($page,$limit,$where);
        $count = BannerModel::getBannerCountWhere($where);
        foreach ($list as &$item){
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
        }
        unset($item);

        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");

    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addBanner(Request $request)
    {
        $param = $request->param();
        $title = $param['title'];
        $type_id = $param['type_id'];
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;
        $background = !empty($param['background']) ? $param['background'] : "";

        //图片上传
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "banner/".$type_id."/";
            $img = ImageUtil::i()->upload($file, $path);
        }else{
            return Util::returnJson(400,null,"请上传图片");
        }

        $data = [
            'title'=>$title,
            'type_id'=>$type_id,
            'img'=>!empty($img) ? $img : "",
            'sort'=>$sort,
            'background'=>$background,
            'up_time'=>date("Y-m-d H:i:s",time()),
            'add_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show) && $is_show == "on"){
            $data['is_show'] = 1;
        }
        $id = BannerModel::addBanner($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editBanner(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $title = $param['title'];
        $type_id = $param['type_id'];
        $background = !empty($param['background']) ? $param['background'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($id)){
            return Util::returnJson(self::CODE_ERROR,null,"参数错误");
        }

        $save = [
            'title'=>$title,
            'type_id'=>$type_id,
            'background'=>$background,
            'up_time'=>date("Y-m-d H:i:s",time())
        ];

        //图片上传
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "banner/".$type_id."/" . date("Ymd") . '/';
            $img = ImageUtil::i()->upload($file, $path);
            $save['img'] = $img;
        }

        if(!empty($is_show)){
            if($is_show == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }
        $flag = BannerModel::updateBannerWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delBanner(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try{
            //中间逻辑代码
            $flag = BannerModel::delBannerWhere([['id','=',$id]]);
            if($flag){
                //提交事务
                Db::commit();
                return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
            }else{
                Db::rollBack();
                return Util::returnJson(self::CODE_ERROR,null,"删除失败4");
            }
        }catch (\Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            Db::rollBack();
            return Util::returnJson(self::CODE_ERROR,$e,"删除失败1");
        }
    }

}
